package com.xiaoxin.xinlikesystem.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.xiaoxin.xinlikesystem.common.BaseResponse;
import com.xiaoxin.xinlikesystem.common.ResultUtils;
import com.xiaoxin.xinlikesystem.exception.ErrorCode;
import com.xiaoxin.xinlikesystem.exception.ThrowUtils;
import com.xiaoxin.xinlikesystem.model.dto.blog.BlogQueryRequest;
import com.xiaoxin.xinlikesystem.model.vo.Blog.BlogVO;
import com.xiaoxin.xinlikesystem.service.BlogService;
import io.swagger.v3.oas.annotations.Operation;
import jakarta.annotation.Resource;
import jakarta.servlet.http.HttpServletRequest;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @version 1.0
 * @Author 武小新
 * @Date 2025/5/12 16:12
 * @注释 博客层
 */
@RestController
@RequestMapping("/blog")
public class BlogController {

    @Resource
    private BlogService blogService;

    /**
     * 根据Id获取博客内容
     */
    @GetMapping("/get")
    @Operation(summary = "获取博客内容", description = "根据博客ID获取博客内容")
    public BaseResponse<BlogVO> get(long blogId, HttpServletRequest request) {
        // 1. 校验参数并查询
        ThrowUtils.throwIf(blogId <= 0, ErrorCode.PARAMS_ERROR);
        BlogVO blogVO = blogService.getBlogVOById(blogId, request);

        // 2. 返回结果
        return ResultUtils.success(blogVO);
    }

    /**
     * 分页获取博客封装列表
     */
    @PostMapping("/list/page/vo")
    @Operation(summary = "分页获取博客", description = "分页获取博客内容")
    public BaseResponse<Page<BlogVO>> listBlogVOPage(@RequestBody BlogQueryRequest userQueryRequest, HttpServletRequest request) {
        ThrowUtils.throwIf(userQueryRequest == null, ErrorCode.PARAMS_ERROR);
        long current = userQueryRequest.getCurrent();
        long pageSize = userQueryRequest.getPageSize();
        Page<BlogVO> userVOPage = blogService.getBlogVOPage(current, pageSize, request);

        return ResultUtils.success(userVOPage);
    }


}
